package veilingsite.servlets.nl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Formatter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import veilingsite.beans.nl.UserBean;
import veilingsite.dao.nl.UserDao;

/**
 * Servlet implementation class InlogServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher view = null;
		UserDao uDao = (UserDao) getServletContext().getAttribute("UserDao");

		String[] url = request.getQueryString().split( "=" );
		if(url[0].equals("page") && url[1].equals("login")){
			UserBean user = uDao.userLogin(request.getParameter("username"), request.getParameter("password"));
			
			String gebruikersnaam = request.getParameter("username");
			String wachtwoord = request.getParameter("password");
			if(gebruikersnaam.equals("") || wachtwoord.equals("")){
				request.setAttribute( "message", "warning");
				request.setAttribute( "messageValue", "inloggegevens zijn niet ingevuld");
				view = request.getRequestDispatcher("/WEB-INF/login.jsp");
			}else{
				if(user == null){
					request.setAttribute( "message", "error");
					request.setAttribute( "messageValue", "verkeerde inloggegevens");
					view = request.getRequestDispatcher("/WEB-INF/login.jsp");
				}else{
					setSession(request, user);
					request.setAttribute( "message", "succes");
					request.setAttribute( "messageValue", "ingelogged");
					view = request.getRequestDispatcher( "/WEB-INF/index.jsp" );
				}
			}
		}else if(url[0].equals("page") && url[1].equals("addUser")){
			//voeg gebruiker toe
			if(addKlant(request, uDao)){
				view = request.getRequestDispatcher("/WEB-INF/login.jsp");
				request.setAttribute( "message", "succes");
				request.setAttribute( "messageValue", "gebruiker is toegevoegd, u kunt nu inloggen.");
			} else {
				request.setAttribute( "message", "warning");
				request.setAttribute( "messageValue", "invoer niet geldig.");
				view = request.getRequestDispatcher("/WEB-INF/newUser.jsp");
			}
		}else if (url[0].equals("page") && url[1].equals("logout")){
			unsetSession(request);
			view = request.getRequestDispatcher("/WEB-INF/index.jsp");
			request.setAttribute( "message", "succes");
			request.setAttribute( "messageValue", "uitgelogged.");
		} else {
			view = request.getRequestDispatcher("/WEB-INF/index.jsp");
		}
		view.forward( request, response );
	}
	
	private boolean addKlant(HttpServletRequest request, UserDao uDao){
		boolean succeeded = false;
		if(request.getParameter("password").equals(request.getParameter("confirm"))){
			String username = request.getParameter("username");
			String name = request.getParameter("name");
			uDao.createKlant(username, request.getParameter("password"), name);
			succeeded = true;
		}
		return succeeded;
	}
	
	private void setSession( HttpServletRequest request, UserBean user){
		HttpSession session = request.getSession();
		session.setAttribute("UserBean", user);
	}
	
	private void unsetSession(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.invalidate();
	}
}
